package java8.chapter07.test;

import java.util.concurrent.ForkJoinPool;
import java.util.function.Function;

public class ParellelStreamsHarness {
	
	public static ForkJoinPool FORK_JOIN_POOL = new ForkJoinPool();

	public static void main(String[] args) {
//		System.out.println("Iterative Sum done in: " + measurePerf(ParallelStreams::iterativeSum, 10_000_000L) + " msecs");
//		System.out.println("Sequential Sum done in " + measurePerf(ParallelStreams::sequentialSum, 10_000_000L) + " msecs");
//		System.out.println("Parallel forkJoinSum done in " + measurePerf(ParallelStreams::parallelSum, 10_000_000L) + " msecs");
//		System.out.println("Range forkJoinSum done in " + measurePerf(ParallelStreams::rangedSum, 10_000_000L) + " msecs");
//		System.out.println("Parallel range forkJoinSum done in " + measurePerf(ParallelStreams::parallelRangedSum, 10_000_000L) + " msecs");
//		System.out.println("ForkJoin sum done in " + measurePerf(ForkJoinSumCalculator::forkJoinSum, 10_000_000L) + " msecs");
//		System.out.println("SideEffect sum done in " + measurePerf(ParallelStreams::sideEffectSum, 10_000_000L) + " msecs");
		System.out.println("SideEffect parallel sum done in " + measurePerf(ParallelStreams::sideEffectParallelSum, 10_000_000L) + " msecs");
	}

	private static <T, R> long measurePerf(Function<T, R> f, T input) {
		long fastest = Long.MAX_VALUE;
		for (int i = 0; i < 10; i++) {
			long start = System.nanoTime();
			R result = f.apply(input);
			long duration = (System.nanoTime() - start) / 1_000_000;
			System.out.println("Result: " + result);
			if (duration < fastest)
				fastest = duration;
		}
		return fastest;
	}

}